package com.original.simuladoresws.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.original.simuladoresws.dao.GenericDao;
import com.original.simuladoresws.dao.HibernateUtil;

public class GenericDaoImpl<T> implements GenericDao<T> {
	
	SessionFactory sessionFactory;
	
	public GenericDaoImpl() {
		super();
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	@Override
	public Serializable create(T instance) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(instance);
		tx.commit();
		Serializable id = session.getIdentifier(instance);
		session.close();
		return (Serializable) id;

	}

	@Override
	public Serializable update(T instance) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.update(instance);
		tx.commit();
		Serializable id = session.getIdentifier(instance);
		session.close();
		return (Serializable) id;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Serializable delete(Class instance, Serializable id) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		T t = (T) session.load(instance, id);
		session.delete(t);
		tx.commit();
		Serializable ids = session.getIdentifier(t);
		session.close();
		return (Serializable) ids;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance, Integer id) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " WHERE id = " + id).list();
		session.close();
		return lsT;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance, String id) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " WHERE id = " + id).list();
		session.close();
		return lsT;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString())).list();
		session.close();
		return lsT;
	}

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<T> lastestObject(Class instance) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " A ORDER BY A.id DESC").setMaxResults(1).list();
		session.close();
		return lsT;
	}

	/**
	 * get Name of Class
	 * 
	 * @param strInput
	 * @return Class name
	 */
	private String getNameClass(String strInput) {
		String[] spt = strInput.split("\\.");
		return spt[spt.length - 1];
	}
}
